package com.xxgc.eow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xxgc.eow.entity.Company;
import com.xxgc.eow.utils.CosFile;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.xxgc.eow.result.Result;

import java.time.LocalDateTime;
import java.util.List;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xxgc.eow.service.ICollectcompanyService;
import com.xxgc.eow.entity.Collectcompany;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收藏企业表 前端控制器
 * </p>
 *
 * @author Mr.Abel
 * @since 2023-05-23
 */
@Tag(name = "收藏企业表")
@RestController
@RequestMapping("/collectcompany")
public class CollectcompanyController {

    @Resource
    private ICollectcompanyService collectcompanyService;

    /**
     * 查询所有接口
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "查询所有接口", description = "查询收藏企业表所有数据")
    @SaIgnore
    @GetMapping
    public Result<List<Collectcompany>> findAll() {
        return Result.ok(collectcompanyService.list());
    }

    /**
     * 根据id查询数据接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "根据UserId查询数据接口", description = "根据UserId查询")
    @Parameters({
            @Parameter(name = "id",description = "组件id",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/{id}")
    public Result<List<Collectcompany>> findOne(@PathVariable Integer id) {
        List<Collectcompany> collectCompanies = collectcompanyService.getCollectCompaniesByUserId(id);
        for (Collectcompany collectCompany : collectCompanies) {
            List<Company> companys = collectCompany.getCompany();
            for (Company company : companys) {
                if (CosFile.getFile("/images/companyUrl/"+company.getCompanyUrl()).size()>0 ){
                    company.setCompanyUrl(CosFile.getFile("/images/companyUrl/"+company.getCompanyUrl()).get(0));
                }
            }
        }
        return Result.ok(collectCompanies);
    }

    /**
     * 新增和更新接口
     * @param
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "新增接口", description = "新增接口")
    @SaIgnore
    @PostMapping
    public Result save(@RequestBody Collectcompany collectcompany) {
        collectcompanyService.add(collectcompany);
        return Result.ok();
    }

    /**
     * 删除接口
     * @param userId
     * @return
     */
    @ApiOperationSupport(author = "Mr.Abel")
    @Operation(summary = "删除接口", description = "通过userId和companyId删除")
    @Parameters({
            @Parameter(name = "userId",description = "userId",required = true,in=ParameterIn.QUERY),
            @Parameter(name = "companyId",description = "companyId",required = true,in=ParameterIn.QUERY)
    })
    @SaIgnore
    @GetMapping("/del")
    public Result delete(@Parameter Integer userId,@Parameter Integer companyId) {
        Collectcompany collectcompany = new Collectcompany();
        collectcompany.setCompanyId(companyId);
        collectcompany.setUserId(userId);
        collectcompany.setDestroyTime(LocalDateTime.now());
        collectcompany.setTip(1);
        collectcompanyService.updat(collectcompany);
        return Result.ok();
    }


}